﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TPOJ.IProfileDAL;
using TPOJ.Model;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace TPOJ.SQLProfileDAL
{
    public class SQLProfileProvider : ITPOJProfileProvider
    {
        public void InsertAccount(Account account)
        {
            string commandString = "INSERT INTO Account(School, Name, Email) VALUES (@School, @Name, @Email)";
            string connectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString();



            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = connectionString;
                conn.Open();
                SqlCommand command = conn.CreateCommand();
                command.CommandText = commandString;
                SqlTransaction trans = conn.BeginTransaction(IsolationLevel.ReadCommitted);

                try
                {
                    command.Transaction = trans;
                    command.Parameters.Add(new SqlParameter("@School", account.School));
                    command.Parameters.Add(new SqlParameter("@Name",account.Name));
                    command.Parameters.Add(new SqlParameter("@Email",account.Email));
                    command.ExecuteNonQuery();
                    trans.Commit();
                }
                catch (Exception e)
                {
                    trans.Rollback();
                    throw new ApplicationException(e.Message);
                }
                finally
                {
                    conn.Close();
                }
            }
        }
    }
}
